<?php

/**
 * This is the model class for table "store".
 *
 * The followings are the available columns in table 'store':
 * @property integer $id
 * @property integer $productid
 * @property integer $billid
 * @property string $pricebuy
 * @property string $datebuy
 * @property string $pricesell
 * @property string $datesell
 * @property string $created
 * @property string $recommendpricesell
 * @property integer $orderid
 * @property integer $unsoldcount
 *
 * The followings are the available model relations:
 * @property Bills $bill
 * @property Products $product
 */
class Store extends CActiveRecord
{

	public $unsoldcount;
	public $soldcount;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Store the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'store';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('productid, billid, orderid, unsoldcount, soldcount', 'numerical', 'integerOnly'=>true),
			array('pricebuy, pricesell, recommendpricesell', 'length', 'max'=>10),
			array('datebuy, datesell', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, productid, billid, pricebuy, datebuy, pricesell, datesell, created, recommendpricesell, orderid, unsoldcount, soldcount', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bill' => array(self::BELONGS_TO, 'Bills', 'billid'),
			'product' => array(self::BELONGS_TO, 'Products', 'productid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'productid' => 'Productid',
			'billid' => 'Billid',
			'pricebuy' => 'Pricebuy',
			'datebuy' => 'Datebuy',
			'pricesell' => 'Pricesell',
			'datesell' => 'Datesell',
			'created' => 'Created',
			'recommendpricesell' => 'Recommendpricesell',
			'orderid' => 'Orderid',
			'unsoldcount' => 'Count',
			'soldcount' => 'Sold'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('productid',$this->productid);
		$criteria->compare('billid',$this->billid);
		$criteria->compare('pricebuy',$this->pricebuy,true);
		$criteria->compare('datebuy',$this->datebuy,true);
		$criteria->compare('pricesell',$this->pricesell,true);
		$criteria->compare('datesell',$this->datesell,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('recommendpricesell',$this->recommendpricesell,true);
		$criteria->compare('orderid',$this->orderid);
		$criteria->compare('unsoldcount',$this->unsoldcount);
		$criteria->compare('soldcount',$this->soldcount);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public static function countNonSold($stores)
	{
		$count = 0;
		if(count($stores)>0)
		{
			foreach($stores as $store)
			{
				if(is_null($store->pricesell) && is_null($store->datesell)) $count++;
			}
		}
		return $count;
	}
	
	public function scopes()
	{
		return array(
		'unsold'=>array(
          'condition'=>'datesell IS NULL AND pricesell IS NULL',
		  )
		);

	}
	
	public function getTotals($provider)
	{
		$totals = array();
		$sumsold = 0;
		$sumbought = 0;
		$sumsoldcnt = 0;
		$sumboughtcnt = 0;
		$data = $provider->getData();
		foreach($data as $p)
		{	
			$sumsold += $p->pricesell;
			$sumbought += $p->pricebuy;
			$sumsoldcnt++;
			$sumboughtcnt++;
		}
		$totals['sumsold']=$sumsold;
		$totals['sumbought']=$sumbought;
		$totals['sumsoldcnt']=$sumsoldcnt;
		$totals['sumboughtcnt']=$sumboughtcnt;

		return $totals;
	}
}